//
//  PlaceTextView.swift
//  PlaceTextViewDemo
//
//  Created by zhuxuhong on 2018/1/18.
//  Copyright © 2018年 GuessMe. All rights reserved.
//

import UIKit

@IBDesignable
class PlaceTextView: UITextView {

// MARK: - IBOutlets
	fileprivate lazy var placeholderLabel: UILabel = {
		let lb: UILabel = .init(frame: .init(x: 10, y: 10, width: self.bounds.width - 20, height: 20))
		lb.font = self.font
		lb.textColor = .lightGray
		lb.numberOfLines = 1
		lb.text = self.placeholder
		return lb
	}()
	
	fileprivate lazy var clearButton: UIButton = {
		let btn: UIButton = UIButton(type: .system)
		btn.setTitle("×", for: .normal)
		btn.titleLabel?.font = UIFont.systemFont(ofSize: 18)
		btn.setTitleColor(.lightGray, for: .normal)
		btn.backgroundColor = .white
		btn.isHidden = true
		btn.layer.cornerRadius = self.clearButtonSize / 2
		btn.addTarget(self, action: #selector(self.clearInput), for: .touchUpInside)
		return btn
	}()
	
// MARK: - Properties
	fileprivate var clearButtonSize: CGFloat = 24
	
	fileprivate var placeholderColor: UIColor{
		return textColor!.alpha(0.7)
	}
	
    override var attributedText: NSAttributedString!{
        didSet{
            textDidChange()
        }
    }
    
	override var text: String!{
		didSet{
			textDidChange()
		}
	}
	
	
	var placeholder: String? = "请输入..."{
		didSet{
			placeholderLabel.text = placeholder
		}
	}
	
	
	var needsClearButton: Bool = true{
		didSet{
			clearButtonSize = needsClearButton ? 24 : 0
		}
	}
	
	override var textContainerInset: UIEdgeInsets{
		didSet{
			super.textContainerInset = UIEdgeInsets(top: textContainerInset.top, left: textContainerInset.left, bottom: textContainerInset.bottom, right: textContainerInset.right + clearButtonSize + 10)
		}
	}
	
// MARK: - Initial Method
	private func setupUI() {
		self.textContainerInset = super.textContainerInset
		
		if needsClearButton {
			addSubview(clearButton)
		}
		addSubview(placeholderLabel)

		NotificationCenter.default.addObserver(self, selector: #selector(textDidChange), name: NSNotification.Name.UITextViewTextDidChange, object: nil)
	}
	
 
// MARK: - Lifecycle Method
	override func awakeFromNib() {
		super.awakeFromNib()		
		setupUI()
	}
	
	override init(frame: CGRect, textContainer: NSTextContainer?) {
		super.init(frame: frame, textContainer: textContainer)
		setupUI()
	}
	
	required init?(coder aDecoder: NSCoder) {
		super.init(coder: aDecoder)
	}
	
	override func layoutSubviews() {
		super.layoutSubviews()
		
		if needsClearButton{
			layoutClearButton()
		}
		layoutPlaceholderLabel()
	}
	
	deinit {
		NotificationCenter.default.removeObserver(self)
	}
	
// MARK: - Action & IBOutletAction
	@objc func clearInput(){
		text = nil
		textDidChange()
		delegate?.textViewDidChange?(self)
	}
	
	@objc fileprivate func textDidChange(){
		let isEmpty = text == nil || text.isEmpty
		placeholderLabel.text = isEmpty ? placeholder : nil
		if needsClearButton {
			clearButton.isHidden = isEmpty
		}
	}
	
// MARK: - Override Method3
	
// MARK: - Private method
	fileprivate func layoutClearButton() {
		let x = bounds.width - clearButtonSize - 10
		let y = bounds.height / 2 - clearButtonSize / 2 + contentOffset.y
		clearButton.frame = CGRect(origin: .init(x: x, y: y), size: CGSize(width: clearButtonSize, height: clearButtonSize))
	}
	
	fileprivate func layoutPlaceholderLabel() {
		let x = textContainerInset.left + 4
		let width = bounds.width - textContainerInset.left - 4
		placeholderLabel.frame = CGRect(origin: .init(x: x, y: textContainerInset.top), size: CGSize(width: width, height: 24))
	}
	
	

// MARK: - Public Method

}
